var OutputStreamWriter = plus.android.importClass("java.io.OutputStreamWriter");  
//mac_address 蓝牙地址  
//byteStr 二维码内容  

function PrintQrCode(mac_address,byteStr) {  
    if (!mac_address) {  
        mui.toast('请选择蓝牙打印机');  
        return;  
    }  

    main = plus.android.runtimeMainActivity();  
    BluetoothAdapter = plus.android.importClass("android.bluetooth.BluetoothAdapter");  
    UUID = plus.android.importClass("java.util.UUID");  
    uuid = UUID.fromString("00001101-0000-1000-8000-00805F9B34FB");  
    BAdapter = BluetoothAdapter.getDefaultAdapter();  
    device = BAdapter.getRemoteDevice(mac_address);  
    plus.android.importClass(device);  
    bluetoothSocket = device.createInsecureRfcommSocketToServiceRecord(uuid);  
    plus.android.importClass(bluetoothSocket);  

    if (!bluetoothSocket.isConnected()) {  
        console.log('检测到设备未连接，尝试连接....');  
        bluetoothSocket.connect();  
    }  

    console.log('设备已连接');  

    if (bluetoothSocket.isConnected()) {  
      var outputStream = bluetoothSocket.getOutputStream();  
      plus.android.importClass(outputStream);  
      var writer = new OutputStreamWriter(outputStream, 'GBK');   
      plus.android.importClass(writer);  

      //init  
      writer.write(0x1B);  
      writer.write(0x40);  
      writer.flush();  

      var moduleSize = 8;  
      var bytes = plus.android.invoke(byteStr, 'getBytes', 'gbk');  
      var length = bytes.length;  

      console.log(length);  
      //打印二维码矩阵    
      writer.write(0x1D);// init    
      writer.write("(k");// adjust height of barcode    
      writer.write(length + 3); // pl    
      writer.write(0); // ph    
      writer.write(49); // cn    
      writer.write(80); // fn    
      writer.write(48); //    
      writer.write(byteStr);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3);  
      writer.write(0);  
      writer.write(49);  
      writer.write(69);  
      writer.write(48);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3);  
      writer.write(0);  
      writer.write(49);  
      writer.write(67);  
      writer.write(moduleSize);  

      writer.write(0x1D);  
      writer.write("(k");  
      writer.write(3); // pl    
      writer.write(0); // ph    
      writer.write(49); // cn    
      writer.write(81); // fn    
      writer.write(48); // m    

      writer.flush();   
    }  
}